/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uns.fit.gis;

import java.util.Timer;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.LayerConfig;
import uns.fit.gis.mapdrawer.MapCanvas;
import uns.fit.gis.mapdrawer.TestCanvas2;
import uns.fit.gis.mapobj.Segment;
import uns.fit.gis.rms.RSManager;
import uns.fit.gis.serviceapp.Connector;
import uns.fit.gis.serviceapp.task;
import uns.fit.gis.settings.MapSettings;
import uns.fit.gis.ui.BestRouteList;
import uns.fit.gis.ui.FlickrConfigForm;
import uns.fit.gis.ui.GPSConnectionList;
import uns.fit.gis.ui.GridMenuCanvas;
import uns.fit.gis.ui.HelpForm;
import uns.fit.gis.ui.ImageForm;
import uns.fit.gis.ui.InforCellService;
import uns.fit.gis.ui.PhotoForm;
import uns.fit.gis.ui.RecorderForm;
import uns.fit.gis.ui.RegisterForm;
import uns.fit.gis.ui.RouteAssistCanvas;
import uns.fit.gis.ui.RouteList;
import uns.fit.gis.ui.SatelliteCanvas;
import uns.fit.gis.ui.SaveCurPosForm;
import uns.fit.gis.ui.SearchLocationForm;
import uns.fit.gis.ui.SearchPathForm;
import uns.fit.gis.ui.SettingsForm;
import uns.fit.gis.ui.SkyCanvas;
import uns.fit.gis.ui.SplashCanvas;
import uns.fit.gis.ui.UserLocationList;
import uns.fit.gis.ui.VideoCanvas;

/**
 * @author Nguyen Le
 */
public class MapDrawer extends MIDlet {
    
    protected static Display display;
    public SearchPathForm pathForm;
    public ImageForm imageForm;
    public RecorderForm recorderForm;
    public SearchLocationForm locationForm;
    public GPSConnectionList gpsConnectionList;
    public RouteAssistCanvas routeCanvas;
    public BestRouteList bestRouteList;
    public RouteList routeList;
    public MapCanvas mapCanvas;
    public GridMenuCanvas menuCanvas;
    public SettingsForm settingsForm;
    public SkyCanvas skyCanvas;
    public SatelliteCanvas satelliteCanvas;
    public SaveCurPosForm saveCurPosForm;
    public SplashCanvas splashCanvas;
    public UserLocationList userLocationList;
    public FlickrConfigForm configForm;
    public HelpForm helpForm;
    public InforCellService infoCellSerForm;
    private static MapSettings settings;
    
    private static Alert alert;

    protected Timer iTimer;
    
    private static MapDrawer midlet;
    private RegisterForm registerForm;
    
    //public static String FlikcrApiKey = "be0eb14a8d92f2df42dbd152187c48c9";
    //public static String FlickrSecretShared = "7a7ae2bf746bc01a";
    public static String FlikcrApiKey = "93ee6d50a87abbdc25470d04cc352bef";
    public static String FlickrSecretShared = "7eee3b08a37afdf0";
    
    public MapDrawer() {
    	super();
    	midlet = this;
        settings = new MapSettings();
        LocaleManager.initLocalizationSupport();
    	display = Display.getDisplay(this);
    	
    	gpsConnectionList = new GPSConnectionList("", this);
    	splashCanvas = new SplashCanvas(this);
    	LayerConfig.showATM = LayerConfig.showCafe = LayerConfig.showGasStation = LayerConfig.showSchool = LayerConfig.showStore = false;
    }
    
    public static MapDrawer getMidlet() {
    	return midlet;
    }

    public void showRegisterForm() {
        if(registerForm == null)
            registerForm = new RegisterForm("", this);
        display.setCurrent(registerForm);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        showMapCanvas();
        //showTestCanvas();
    }
    
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    	settings.setNumberOfUserLocations(MapCanvas.userLocations.size());
    	settings.save();
    	/*
		new Thread() {
			public void run() {
				FileWrapper.saveUserLocations(Util.STORAGE_FOLDER + Util.USER_LOCATION_FILE_NAME, MapCanvas.userLocations);
			}
		}.start();
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
        RSManager.delete(Util.USER_LOCATION_RECORDSTORE_NAME);
        RSManager.open(Util.USER_LOCATION_RECORDSTORE_NAME);
        RSManager.saveUserLocations(MapCanvas.userLocations);
        RSManager.close();
        System.gc();
        notifyDestroyed();
    }
    
    public void showMapCanvas() {
    	if(mapCanvas == null)
    		mapCanvas = new MapCanvas(this);
    	display.setCurrent(mapCanvas);
    }
 
    public void showTestCanvas() {
    	display.setCurrent(new TestCanvas2());
    }
    
    public void showSearchPathForm() {
    	if(pathForm == null) {
    		pathForm = new SearchPathForm("", this);
    	}
    	display.setCurrent(pathForm);
    }
    
    public void showImageForm(byte[] photoData) {
    	if(imageForm == null) {
    		imageForm = new ImageForm("", this);
    	}
    	imageForm.deleteAll();
    	imageForm.setPhotoData(photoData);
    	display.setCurrent(imageForm);
    }
    
    public void showVideoCanvas() {
    	display.setCurrent(new VideoCanvas(this));
    }
    
    public void showGPSConnectionList(String[] deviceNames) {
    	gpsConnectionList.setDeviceNames(deviceNames);
    	
        MapDrawer.showAlert(alert);
        
    	display.setCurrent(gpsConnectionList);
    }
    
    public void showRecorderForm() {
    	if(recorderForm == null) {
    		recorderForm = new RecorderForm(this);
    	}
    	display.setCurrent(recorderForm);
    }
    
    public void showLocationForm() {
    	if(locationForm == null) {
    		locationForm = new SearchLocationForm("", this);
    	}
    	locationForm.updateCenter();
    	display.setCurrent(locationForm);
    }
    
    public static void showAlert(String message) {
    	if(alert == null)
    		alert = new Alert("MapDrawer");
    	alert.setString(message);
    	display.setCurrent(alert);
    }
    
    public static void showAlert(Alert alert, Displayable nextDisplayable) {
    	if(alert != null)
    		display.setCurrent(alert, nextDisplayable);
    }
    
    public static void showAlert(Alert alert) {
    	if(alert != null)
    		display.setCurrent(alert);
    }
    
    public void updateMapCanvas() {
    	//TODO: do something before repainting
    	mapCanvas.repaint();
    }
    
    public void showBestRouteList() {
    	if(bestRouteList == null) {
    		bestRouteList = new BestRouteList(this);
    	}
    	bestRouteList.createRoute();
    	display.setCurrent(bestRouteList);
    }
    
    public void showRouteList() {
    	if(routeList == null) {
    		routeList = new RouteList(this);
    	}
    	display.setCurrent(routeList);
    }
    
    public void showMenuCanvas() {
    	if(menuCanvas == null) {
    		menuCanvas = new GridMenuCanvas(this);
    	}
    	display.setCurrent(menuCanvas);
    }
    
    public void showRouteAssistCanvas() {
    	if(routeCanvas == null) {
    		routeCanvas = new RouteAssistCanvas(this);
    	}
    	display.setCurrent(routeCanvas);
    }
    
    public void showSettingsForm() {
    	if(settingsForm == null){
    		settingsForm = new SettingsForm("", this);
    	}
    	display.setCurrent(settingsForm);
    }
    
    public MapSettings getSettings() {
    	return settings;
    }
    
    public void showSkyCanvas() {
    	if(skyCanvas == null)
    		skyCanvas = new SkyCanvas();
    	display.setCurrent(skyCanvas);
    }
    
    public void showSatelliteCanvas() {
    	if(satelliteCanvas == null)
    		satelliteCanvas = new SatelliteCanvas();
    	display.setCurrent(satelliteCanvas);
    }
    
    public void showSplashCanvas(Displayable displayable) {
    	System.out.println("SPLASH");
    	splashCanvas.setNextDisplay(displayable);
    	splashCanvas.start();
    }
    
    public void showSaveCurPosForm() {
    	if(saveCurPosForm == null) {
    		saveCurPosForm = new SaveCurPosForm();
    	}
    	display.setCurrent(saveCurPosForm);
    }
    
    public void showPhotoForm(byte[] data) {
    	display.setCurrent(new PhotoForm(data));
    }
    
    public void showConfigForm() {
    	if(configForm == null)
    		configForm = new FlickrConfigForm();
    	display.setCurrent(configForm);
    }
    
    public void showUserLocationList() {
    	if(userLocationList == null)
    		userLocationList = new UserLocationList(this);
    	display.setCurrent(userLocationList);
    }
    
    public void showHelpForm() {
    	if(helpForm == null)
    		helpForm = new HelpForm();
    	display.setCurrent(helpForm);
    }
    public void show(Displayable displayable) {
    	display.setCurrent(displayable);
    }

    public void showInfoCellSer(){
        if(Connector.service != null){
            if (infoCellSerForm == null) {
                infoCellSerForm = new InforCellService(this);
            }
            display.setCurrent(infoCellSerForm);
        }
    }

    public void stopCellSer(){
        Connector.stopConnection();
        mapCanvas.drawArc = false;
    }

    public void connectCellSer(){
        if(Connector.service == null){
            Connector.startConnection();            
        }
    }
}
